package com.zzxkj.blog.controller;

import com.zzxkj.blog.Entity.Type;
import com.zzxkj.blog.search.BlogQuery;
import com.zzxkj.blog.service.BlogService;
import com.zzxkj.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class TypeShowController {
   @Autowired
   private TypeService typeService;
   @Autowired
   protected BlogService blogService;
    @GetMapping("/types/{id}")
    public String type(@PathVariable Long id, @PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blog, Model model){

        List<Type> types = typeService.listTypeTop(10000);
        if(id==-1){
            id=types.get(0).getId();
        }
        blog.setTypeId(id);
        model.addAttribute("types",types);
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        model.addAttribute("activeTypeId",id);
        return "types";
    }
}
